package com.oot.twitter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import com.oot.twitter.info.User;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;

import java.util.ArrayList;

public class StartActivity extends Activity implements OnClickListener{
    // redirect to target Activity
    private Class targetClass = Twitter.class;

    private Handler handler;
    LinearLayout background,signup,signin;
    Drawable  d1,d2,d3;
    int i = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.twitter_start_layout);

		initConnection();
        autoLogin(getCookie());

        background = (LinearLayout)findViewById(R.id.start_background);

        Resources res = getResources();
        d1 = res.getDrawable(R.drawable.signed_out_3);
        //d2 = res.getDrawable(R.drawable.signed_out_2);
       // d3 = res.getDrawable(R.drawable.signed_out_3);
        
        background.setBackgroundDrawable(d1);
        
        signup = (LinearLayout)findViewById(R.id.btnSignUp);
        signup.setOnClickListener(this);
        
        signin = (LinearLayout)findViewById(R.id.btnSignIn);
        signin.setOnClickListener(this);
        
       // handler = new Handler();
		//handler.postDelayed(runnable, 0);


    }

    private String getCookie() {
        // get cookie for default user
        SharedPreferences settings = getSharedPreferences(GlobalData.COKKIE_FILE, MODE_PRIVATE);
        String default_user = settings.getString(GlobalData.DEFAULT_USER, "");
        return settings.getString(default_user, "");

    }

	/**
	 * Init connection for application
	 * Set timeout
	 */
	private void initConnection(){
		HttpParams httpParas = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParas, 5000);
		HttpConnectionParams.setSoTimeout(httpParas, 5000);
		GlobalData.client = new DefaultHttpClient(httpParas);
	}

    private boolean autoLogin(String cookieString) {
        if (cookieString.equals("")) return false;
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie     = new BasicClientCookie("PHPSESSID", "name");
        cookie.setDomain(ConnectionTwitter.DOMAIN_NAME);
        cookie.setValue(cookieString);
        cookie.setVersion(0);
        cookie.setPath("/");

        cookieStore.addCookie(cookie);

        // set cookie to connection
        GlobalData.client.setCookieStore(cookieStore);
        ConnectionTwitter.asyncHttpClient.setCookieStore(cookieStore);
        System.out.println("Cookie String :" + cookieStore.toString());
		JSONObject jsonObject  = ConnectionTwitter.sendRequestJSon(ConnectionTwitter.AUTOLOGIN_URL, new ArrayList<NameValuePair>());
        GlobalData.currentUser = new User(jsonObject);
		System.out.println("ffffffff");
        if (GlobalData.currentUser.getUser_id().equals("")) return false;

        // goto target class
        Intent target = new Intent(StartActivity.this, targetClass);
        startActivity(target);
        finish();
        return true;
    }

    private final Runnable runnable = new Runnable() {
        public void run() {
        	if(i == 1)
        		background.setBackgroundDrawable(d1);
        	if(i == 2)
        		background.setBackgroundDrawable(d2);
        	if(i == 3)
        		background.setBackgroundDrawable(d3);
        	i++;
        	if(i > 3)
        		i = 1;
            handler.postDelayed(runnable, 10000);
        }
    };
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.btnSignUp:
				Intent i = new Intent(StartActivity.this, SignUp.class);
				startActivity(i);
				break;
			case R.id.btnSignIn:
				Intent i1 = new Intent(StartActivity.this, SignIn.class);
				startActivityForResult(i1,0);
				break;
		
		}
		
	}    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if(resultCode == 2){
	        finish();
	    }
	}	
}
